package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.ReaderJumpConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.ReaderInputParams;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.ShelfGoodBook;

import java.util.List;

/**
 * Title:
 * Description:
 * Copyright: Copyright(c)2018
 * Company: 看书网
 * CreateTime: 2018/5/14 14:47
 *
 * @author EvanHe
 * @version 3.0
 */
public class BookShelfGoodBookAdapter extends BaseQuickAdapter<ShelfGoodBook> {


    public BookShelfGoodBookAdapter(Context context, List<ShelfGoodBook> data) {
        super(context, data);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_shelf_good_book_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, ShelfGoodBook item, final int pos) {
        if (item.adView != null) {
            ViewParent parent = item.adView.getParent();
            if (parent != null) {
                ViewGroup viewGroup = (ViewGroup) parent;
                viewGroup.removeView(item.adView);
            }
            FrameLayout layout = (FrameLayout) holder.convertView;
            layout.removeAllViews();
            layout.addView(item.adView);
            return;
        }
        holder.setText(R.id.book_title, item.book_title);
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
        holder.getConvertView().setOnClickListener(v -> {
            ReaderInputParams params = new ReaderInputParams();
            params.book_id = item.book_id;
            ReaderJumpConfig.startReaderActivity(mContext, params);

            AdPresenter.pvuvStatics(item.position,R.string.HSTUIJIAN_POSITION1,R.string.HSTUIJIAN_POSITION2,
                    R.string.HSTUIJIAN_POSITION3,R.string.HSTUIJIAN_POSITION4,
                    R.string.HSTUIJIAN_POSITION5,R.string.HSTUIJIAN_POSITION6,
                    R.string.HSTUIJIAN_POSITION7,R.string.HSTUIJIAN_POSITION8,
                    R.string.HSTUIJIAN_POSITION9,R.string.HSTUIJIAN_POSITION10,
                    R.string.HSTUIJIAN_POSITION11,R.string.HSTUIJIAN_POSITION12,
                    R.string.HSTUIJIAN_POSITION13,R.string.HSTUIJIAN_POSITION14);
        });
    }


    @Override
    public int getItemViewType(int position) {
        int viewType = super.getItemViewType(position);
        if(viewType == 0 ){
            ShelfGoodBook item = getData().get(position - getHeaderViewsCount());
            if (item.adView != null) {
                viewType = EMPTY_CONTAINER;
            }
        }
        return viewType;
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder result = super.onCreateDefViewHolder(parent, viewType);
        if (viewType == EMPTY_CONTAINER) {
            FrameLayout layout = (FrameLayout) mLayoutInflater.inflate(R.layout.layout_empty_container, parent, false);
            result = new BaseViewHolder(layout);
        }
        return result;
    }
}
